package mao.thinkinjava.array;

import java.util.Arrays;

public class Generated {
   public static <T> T[] array(T[] a,Generator<T> gen){
       for(int i=0;i<a.length;i++){
           a[i] = gen.generate();
       }
       return a;
   }

   public static <T> T[] array(Class<T> clazz,Generator<T> gen,int size){
       //这样行不通，因为类型擦除；
      // T[] a = new T[size];
       T[] a = (T[])java.lang.reflect.Array.newInstance(clazz,size);
       for(int i=0;i<a.length;i++){
           a[i] = gen.generate();
       }
       return a;
   }

    public static void main(String[] args) {
        Character[] chars = Generated.array(new Character[10], new CountingGenerator.Character());
        System.out.println(Arrays.toString(chars));
        String[] ss = Generated.array(String.class, new CountingGenerator.String(),10);
        System.out.println(Arrays.toString(ss));
    }
}
